import { NextResponse } from "next/server";
import { db } from "@/lib/db";

// login
export async function POST(request) {
    const res = await request.json();
    const sql = `SELECT COUNT(*) AS count FROM users WHERE username = '${res.username}' AND password = '${res.password}'`;
    
    try {
        const results = await new Promise((resolve, reject) => {
            db.query(sql, (err, results) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(results);
                }
            });
        });

        const count = results[0].count;
        if (count === 1) {
            return NextResponse.json({ msg: "登录成功", status: 200 });
        } else {
            return NextResponse.json({ msg: "用户名或密码错误", status: 500 });
        }
    } catch (error) {
        return NextResponse.json({ msg: "登录失败", status: 500 });
    }
}
